<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部组件 -->
      <head-tool>
        <div class="btns" slot="after">
          <el-button
            type="primary"
            size="small"
            @click="$router.push({ path: '/import', query: { type: 'emp' } })"
            >导入</el-button
          >
          <el-button type="success" size="small" @click="importExcel"
            >导出</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus "
            @click="addEmp"
            >新增员工</el-button
          >
        </div>
      </head-tool>
      <!-- 员工列表 -->
      <el-card>
        <el-table :data="tableData">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="staffPhoto" label="头像" style="width: 80px">
            <template slot-scope="{ row }">
              <img
                v-imgerror="defaulImg"
                :src="row.staffPhoto"
                alt=""
                style="width: 60px"
              />
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名"> </el-table-column>
          <el-table-column prop="mobile" label="手机号"> </el-table-column>
          <el-table-column prop="workNumber" label="工号"> </el-table-column>
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template slot-scope="{ row }">
              {{ row.formOfEmployment | hireType }}
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门">
          </el-table-column>
          <el-table-column prop="timeOfEntry" label="入职时间">
            <template slot-scope="{ row }">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="enableState" label="状态">
            <template slot-scope="{ row }">
              <el-switch
                v-model="row.enableState"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="230px" fixed="right">
            <div
              style="display: flex; justify-content: space-between"
              slot-scope="{ row }"
            >
              <el-link
                @click="
                  $router.push({
                    path: '/employees/detail',
                    query: { id: row.id },
                  })
                "
                type="primary"
                >查看</el-link
              >
              <el-link type="primary">转正</el-link>
              <el-link type="primary">调岗</el-link>
              <el-link type="primary">离职</el-link>
              <el-link type="primary">角色</el-link>
              <el-link type="primary" @click="delEmp(row.id)">删除</el-link>
            </div>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @current-change="pageChange"
          :current-page="pageInfo.page"
          :page-size="pageInfo.size"
          layout="total, prev, pager, next"
          :total="pageInfo.total"
          style="text-align: center"
        />
      </el-card>
      <!-- 对话框 -->
      <add-emp :dialogShow.sync="dialogShow" />
    </div>
  </div>
</template>

<script>
// 导入请求
import { getEmpList, delEmp } from "@/api/employees";
// 导入枚举值
import constant from "@/api/constant/employees";
// 对话框
import addEmp from "./components/add-emp.vue";
export default {
  components: {
    addEmp,
  },
  data() {
    return {
      dialogShow: false,
      defaulImg: require("@/assets/common/bigUserHeader.png"),
      tableData: [],
      pageInfo: {
        page: 1, //当前页
        size: 10, // 每页多少个
        total: 0, //总量
      },
    };
  },
  created() {
    // 调用请求
    this.getEmpList();
  },
  methods: {
    // 获取员工详细数据
    async getEmpList() {
      let res = await getEmpList(this.pageInfo);
      // console.log(res);
      this.tableData = res.data.rows;
      // 转换swith按钮状态
      this.tableData.forEach((item) => {
        item.enableState = item.enableState == 1 ? true : false;
      });
      this.pageInfo.total = res.data.total;
    },
    // 删除员工
    async delEmp(id) {
      try {
        await this.$confirm("确认要删除吗?");
        await delEmp(id);
        this.$message.success("删除成功");
        //判断是否最后一页删除后刷新上一页
        if (this.tableData.length == 1 && this.pageInfo.page != 1) {
          this.pageInfo.page--;
        }
        this.getEmpList();
      } catch (error) {
        console.log(error);
      }
    },
    // 页码切换
    pageChange(page) {
      this.pageInfo.page = page;
      // 加载数据
      this.getEmpList();
    },
    // 对话框点击事件
    addEmp() {
      this.dialogShow = true;
    },
    async importExcel() {
      // 请求所有员工数据
      let res = await getEmpList({
        page: 1,
        size: this.pageInfo.total,
      });
      let list = res.data.rows;
      const headers = {
        手机号: "mobile",
        姓名: "username",
        入职日期: "timeOfEntry",
        聘用形式: "formOfEmployment",
        转正日期: "correctionTime",
        工号: "workNumber",
        部门: "departmentName",
      };
      let keys = Object.keys(headers);
      // 通过map获得新数组

      let bigArr = list.map((item) => {
        let arr = [];
        // 遍历每一个item对象 生成新数组加到arr中
        for (let key in headers) {
          // 根据中文key转换为英文key
          let engkeys = headers[key];
          arr.push(item[engkeys]);
        }
        return arr;
      });
      import("@/vendor/Export2Excel").then((excel) => {
        excel.export_json_to_excel({
          header: keys, //表头 必填
          data: bigArr, //具体数据 必填
          filename: "员工信息", //非必填
          autoWidth: true, //非必填
          bookType: "xlsx", //非必填
        });
      });
    },
  },
  // 过滤枚举
  filters: {
    hireType(val) {
      let list = constant.hireType.filter((item) => item.id == val);
      return list[0] ? list[0].value : "未知";
    },
  },
};
</script>

<style>
</style>